﻿using System;
using System.Timers;
using SharpSvn;

namespace SvnMonitor
{
    class RepositoryMonitor : IRepositoryMonitor
    {
        private readonly string projectName;
        private readonly string workingCopy;

        private readonly Timer timer;
        private const double timerInterval = 30000;

        public RepositoryMonitor(string projectName, string workingCopy)
        {
            this.projectName = projectName;
            this.workingCopy = workingCopy;

            timer = new Timer();
            timer.Elapsed += timer_Elapsed;
            timer.AutoReset = false;
        }

        public void Start()
        {
            // start immediatly
            timer.Interval = 1;
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
        }

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Stop();

            try
            {
                SvnWorkingCopyVersion workingCopyVersion;
                using (var workingCopyClient = new SvnWorkingCopyClient())
                {
                    workingCopyClient.GetVersion(workingCopy, out workingCopyVersion);
                }

                using (SvnClient client = new SvnClient())
                {
                    var uri = client.GetUriFromWorkingCopy(workingCopy);
                    SvnUriTarget uriTarget = new SvnUriTarget(uri);
                    SvnInfoEventArgs info;
                    client.GetInfo(uriTarget, out info);

                    Logger.Instance.Debug(string.Format("Repository Monitor. Working Copy \"{0}\". LastChangeRevision: {1}", workingCopy, info.LastChangeRevision));

                    if (workingCopyVersion != null)
                    {
                        if (workingCopyVersion.End != info.LastChangeRevision)
                        {
                            if (!MonitorData.IsChanged(workingCopy))
                            {

                                // TODO: move to separate class (here just need to call MonitorData.MarkAsChanged(workingCopy))
                                ViewManager.ShowNotification(new NotificationVM(info, this));
                                MonitorData.MarkAsChanged(workingCopy);
                            }
                        }
                        else
                        {
                            MonitorData.MarkAsUnchanged(workingCopy);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Error("Repository Monitor. Unable to get info.", ex);
            }
            finally
            {
                timer.Interval = timerInterval;
                timer.Start();
            }
        }

        #region IRepositoryMonitor

        public void CheckNow()
        {
            Start();
        }

        public string ProjectName
        {
            get
            {
                return projectName;
            }
        }

        public string WorkingCopy
        {
            get
            {
                return workingCopy;
            }
        }

        #endregion
    }
}
